<?php include("header_liga_required.php"); ?>
<?php
//support for importing multiple DBF files at a time
//the key is the location of the DBF file & the value is the table to import
$dbf_files =  array('PLAYER.DBF' => 'playernew');

//$connect = mysql_connect(DB_SERVER, DB_USER, DB_PWD);
//mysql_select_db(DB_CATALOG);

$db_uname = DB_USER;
$db_passwd = DB_PWD;
$db = DB_CATALOG;
//$conn = mysql_p_connect($db_uname, $db_passwd, $db);
$conn = mysql_connect(DB_SERVER, DB_USER, DB_PWD);
mysql_select_db($db);

if (mysql_query("delete from $db.playernew", $conn)) {
  foreach ($dbf_files as $key => $val){
    import_dbf2($db, $val, $key);
  }
}

function import_dbf2($db, $table, $dbf_file)
{
    global $conn;
    if (!$dbf = dbase_open ($dbf_file, 0)){ die("Could not open $dbf_file for import."); }
    $num_rec = dbase_numrecords($dbf);
    $num_fields = dbase_numfields($dbf);
    $fields = array();
    $errors = "";

    for ($i=1; $i<=$num_rec; $i++){
        $row = @dbase_get_record_with_names($dbf,$i);
        $q = "insert into $db.$table values (";
        foreach ($row as $key => $val){
            if ($key == 'deleted'){ continue; }
            $q .= "'" . addslashes($val) . "',";
        }
        if (isset($extra_col_val)){ $q .= "'$extra_col_val',"; }
        $q = substr($q, 0, -1);
        $q .= ')';
        //if the query failed - go ahead and print a bunch of debug info
        if (!$result = mysql_query($q, $conn)){
            $errors .= mysql_error() . "$q<br>";
        }
    }

    $q = "UPDATE zt_player zp SET zp.player_key = (SELECT pn.matricule
          FROM playernew pn, player p WHERE p.player_key = pn.matricule
          AND p.player_id = zp.player_id)";
    $result = mysql_query($q, $conn);

    $q = "SELECT player.player_id, player.player_name, player.club_id FROM zt_player zp,
        player WHERE zp.player_id = player.player_id AND zp.player_key IS NULL";
    $result = mysql_query($q, $conn);
    if (mysql_num_rows($result) >= 1) {
      while ($row = mysql_fetch_assoc($result)) {
        $playerId = $row['player_id'];
        $playerName = $row['player_name'];
        $club = $row['club_id'];
        $q = sprintf("UPDATE zt_player zp SET zp.player_key = (SELECT matricule
            FROM playernew WHERE UCASE(nom_prenom) = UCASE('%s') AND club = %u)
            WHERE zp.player_id = %u", trim($playerName), $club, $playerId);
        mysql_query($q, $conn);
        //echo $q."br/>";
      }
    }
}
?>

<table height="100%" cellSpacing="0" cellPadding="0" width="100%" border="0" bgcolor="#FFFFFF">
	<tr>
		<td id="leftFiller" valign="top" height="100%" rowspan="3" bgcolor="#666666">
			<table border="0" cellpadding="0" cellspacing="0" height="100%" width="8px">
				<tr><td>&nbsp;</td></tr>
			</table>
		</td>
		<td id="header" valign="top" width="100%">
			<table border="0" width="100%" cellpadding="3" cellspacing="0" bgcolor="#10425b">
                <tr>
                    <td bgcolor="#666666" width="40%">
                        <?php include("inc_title.html"); ?>
                    </td>
                    <td bgcolor="#666666" align="right" valign="top">&nbsp;
                          <span class="headerSmall">Huidige versie PLAYER.DBF: <?=CURRENT_PLAYER_DBF?>
                        <?php
                        if (isset($user) && $user->club_id == 100) {
                        ?>
                        <br/>
                        <a class="headerSmall" href="import_player.php">laad andere versie</a>
                        <?php
                        }
                        ?>
                        </span>
                    </td>
                </tr>
            </table>
            <table align="center" border="0" width="100%" height="90%" cellpadding="0" cellspacing="0" style="border-top:#BCDD10 4px solid;">
                <tr>
                    <?php include("inc_sublinks.php"); ?> 
                </tr>
				<tr><td colspan="2" nowrap bgcolor="#BCDD10" height="4"><img src="../../assets/images/spacer.gif" alt="" height="1" width="1"></td></tr>
				<tr>
					<td>&nbsp;</td>
					<td valign="top">
						<table align="left" cellspacing="10" width="100%">
							<tr>
								<td>
                  <span class="blackHeaderLarge">Zilveren Toren <?=CURRENT_ZT_YEAR?> - Player import</span>
                  <br/>&nbsp;<br/>
                  <?php
                    $q = "SELECT matricule FROM playernew";
                    $result = mysql_query($q, $conn);
                    $totalPlayers = mysql_num_rows($result);
                  ?>
                  <strong>Totaal aantal spelers :</strong> <?=$totalPlayers;?>
                  <br/>
                  <?php
                    $q = "SELECT p.player_name, p.club_id, zp.player_list_id, zp.index_nr FROM zt_player zp,
                        player p WHERE p.player_id = zp.player_id AND zp.player_key IS NULL
                        ORDER BY p.club_id";
                    $result = mysql_query($q, $conn);
                    $invalidPlayers = mysql_num_rows($result);
                  ?>
                  <strong>Aantal niet-speelgerechtigde spelers op spelerslijsten:</strong> <?=$invalidPlayers;?>
                  <br/>
                  <?php
                    if (mysql_num_rows($result) >= 1) {
                  ?>
                  <table width="90%" border="0" cellpadding="2" cellspacing="0">
                    <tr>
                      <td style="border-bottom:#DDDDDD solid 1px"><strong>Naam</strong></td>
                      <td style="border-bottom:#DDDDDD solid 1px"><strong>Club</strong></td>
                      <td style="border-bottom:#DDDDDD solid 1px"><strong>Spelerslijst</strong></td>
                    </tr>
                  <?php
                      while ($row = mysql_fetch_assoc($result)) {
                  ?>
                    <tr>
                      <td style="border-bottom:#DDDDDD solid 1px"><?=$row['player_name']?></td>
                      <td style="border-bottom:#DDDDDD solid 1px"><?=$row['club_id']?></td>
                      <td style="border-bottom:#DDDDDD solid 1px"><?=$row['player_list_id']?></td>
                    </tr>
                  <?php
                      }
                    }
                  ?>
                  </table>
                </td>
							</tr>
						</table>
					</td>
				</tr>
				<tr>
			</table>
		</td>
	</tr>
</table>

<?php include("footer.php"); ?>